<!DOCTYPE html>
<html>
<head>
	<link href="styles.css" rel="stylesheet" type="text/css">
	<title>Map of Deposits</title>
</head>
<body>
	<?php
		// include of required files
		require_once ("constants.php");
		require_once ("planets_class.php");
		require_once ("deposits_class.php");
		require_once ("functions.php");
		
		// set the working view to "list"
		$view = "map";

		// input parameters from post
		$planet_id = $_POST['planet_id'];
		if(isset($_POST['db_action'])) {$db_action = $_POST['db_action'];}
		if(isset($_POST['dep_id'])) {
			$dep_id = $_POST['dep_id'];
			//if deposit id is set, creates a new deposit instance
			$deposit = new deposits($dep_id);
		}
		if(isset($_POST['dep_size'])) {$dep_size = $_POST['dep_size'];}
		if(isset($_POST['x_coord'])) {$x_coord = $_POST['x_coord'];}
		if(isset($_POST['y_coord'])) {$y_coord = $_POST['y_coord'];}
		if(isset($_POST['material'])) {$material = $_POST['material'];}
		
		//creates a new planet instance
		$planet = new planets($planet_id);
		
		// retrieves planet data from the database
		$planet_data = $planet->ReadPlanet();
		
		if($planet_data){
			
			// html header
			echo "<h1 align=\"center\">Planet Name: ".$planet_data['name']."</h1>\n";
			echo "<h2 align=\"center\">Map of Deposits</h2><br />\n";

			// check the db_action (insert, delete, update) and call the appropiate function
			if(isset($db_action)){

				if($db_action === "delete"){
					
					// if the action is "delete", deletes the deposit $dep_id
					if($delete_error = $deposit->DeleteDeposit()){
						
						echo "<p align=\"center\"><strong>Error deleting deposit: " .$delete_error."</strong></p><br>";
						
					} else {
						
						echo "<p align=\"center\"><strong>Deposit deleted</strong></p>";

					} 
					
				}
									
				if($db_action === "insert"){
										
					// if the action is "insert", insert a new deposit.
					if(depositIsValid($material, $dep_size, $x_coord, $y_coord, $planet_data['size']))	{
						if($insert_error = deposits::InsertDeposit($planet_id, $x_coord, $y_coord, $material, $dep_size, DB_USER)){
						
							echo "<p align=\"center\"><strong>Error creating new deposit: " .$insert_error."</strong></p>";	
						} 
						else {
						
							echo "<p align=\"center\"><strong>New deposit created in (".$x_coord.", ".$y_coord."): " . $dep_size . " units of " . $material."</strong></p>";
						}
					}									
				}
				
				if($db_action === "update"){
					
					// if the action is "update", update the deposit.
					if(depositIsValid($material, $dep_size)) {
						if($update_error = $deposit->UpdateDeposit($material, $dep_size, DB_USER)){
							
							echo "<p align=\"center\"><strong>Error updating new deposit: " .$update_error."</strong></p>";
							
						} else {
							
							echo "<p align=\"center\"><strong>Deposit updated: " . $dep_size . " units of " . $material."</strong></p>";
						}
					}
				}
			
			}
			
			// retrieves all the deposits of the planet	in the form of a bidimensional array $data['x']['y']
			$data = $planet->PlanetMap();

			// creates a "switch to map view" button
			echo "<p><FORM METHOD=\"post\" ACTION=\"listDepos.php\" style=\"margin-left: auto; margin-right: auto; text-align: center;\">\n";
			echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">\n";
			echo "<input type=\"submit\" value=\"List View\" name=\"editar\">\n";
			echo "</FORM></p><br />\n";
			
			// creates the map of deposits
			echo "<table border=1 RULES=ALL FRAME=BOX align=\"center\" cellspacing=\"0\" cellpadding=\"0\">\n";
			
			// for each row
			for($x=0; $x<$planet_data['size']; $x++){
				
				echo "<tr>\n";
				
				// for each cell in the row
				for($y=0; $y<$planet_data['size']; $y++){
					
					echo "<td width=27 height=27>";
					
					/* If there is a deposit in the $x, $y cell, put an image of the type of deposit
					 * and a link to deposit.php for editing/deleting of the deposit.
					 * If there is no deposit in the cell, creates an empty-image cell with a link to
					 * AddDeposit.php for deposit creation
					 */
					if(isset($data[$x][$y])){
						
						$deposito = $data[$x][$y];
						$title = $deposito['size']." units of ".$deposito['mat']." at (".$x.", ".$y.")";
						$title .= " - Click to Edit/Delete";
						
						echo "<FORM METHOD=\"post\" ACTION=\"deposit.php\">";
						echo "<input type=\"hidden\" name=\"dep_id\" value=".$deposito['id'].">";
						echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">";
						echo "<input type=\"hidden\" name=\"planet_name\" value=".$planet_data['name'].">";
						echo "<input type=\"hidden\" name=\"planet_size\" value=".$planet_data['size'].">";
						echo "<input type=\"hidden\" name=\"planet_view\" value=".$view.">";
						echo "<input class=\"map\" type=\"image\" align=\"center\" title=\"".$title."\" src=\"gif/".$deposito['mat'].".gif\" name=\"submit\">";
						echo "</FORM>";
						
					} else {
						$title = "Click to add a deposit in (".$x.", ".$y.")";
						
						echo "<FORM METHOD=\"post\" ACTION=\"addDeposit.php\">";
						echo "<input type=\"hidden\" name=\"x_coord\" value=".$x.">";
						echo "<input type=\"hidden\" name=\"y_coord\" value=".$y.">";
						echo "<input type=\"hidden\" name=\"planet_id\" value=".$planet_id.">";
						echo "<input type=\"hidden\" name=\"planet_name\" value=".$planet_data['name'].">";
						echo "<input type=\"hidden\" name=\"planet_size\" value=".$planet_data['size'].">";
						echo "<input type=\"hidden\" name=\"planet_view\" value=".$view.">";
						echo "<input class=\"map\" type=\"image\" align=\"center\" title=\"".$title."\" src=\"gif/Empty.gif\" name=\"submit\">";
						echo "</FORM>";

					}
					echo "</td>";
				}
				echo "</tr>\n";
			}
			echo "</table>\n";
		
		}

	?>
	
	<br>
	<p align="center"><a href="index.php">Back to List of Planets</a></p>
	
</body>
</html>
